//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "LmFixedVolatilityModel.h"
using namespace Cephei::QL::Legacy::Libormarketmodels;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/Parameter.h>
#include <gen/QL/Legacy/Libormarketmodels/LmVolatilityModel.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Models;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::CLmFixedVolatilityModel (Cephei::QL::Math::IArray^ volatilities, Cephei::Core::IVector<Double>^ startTimes) : CLmVolatilityModel(CLmFixedVolatilityModel::typeid)
{
    CArray^ _Cvolatilities;
    try
    {
#ifdef HANDLE
        _phLmFixedVolatilityModel = NULL;
#endif
        _Cvolatilities = safe_cast<CArray^> (volatilities);
        _Cvolatilities->Lock();
        QuantLib::Array& _volatilities = static_cast<QuantLib::Array&> (_Cvolatilities->GetReference ()); 
        startTimes->Lock();
        INativeVector<Double>^ _NCIstartTimes = startTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCstartTimes = safe_cast<CDoubleVector^>(_NCIstartTimes);
        std::vector<QuantLib::Time>& _startTimes = static_cast<std::vector<QuantLib::Time>&> (_NCstartTimes->GetReference ());
        _ppLmFixedVolatilityModel = new boost::shared_ptr<QuantLib::LmFixedVolatilityModel> (new QuantLib::LmFixedVolatilityModel ( _volatilities,  _startTimes ));
        SetLmVolatilityModel (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmFixedVolatilityModel));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cvolatilities != nullptr) _Cvolatilities->Unlock();
        if (startTimes != nullptr) startTimes->Unlock();    //not optional
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::CLmFixedVolatilityModel (boost::shared_ptr<QuantLib::LmFixedVolatilityModel>& childNative, Object^ owner) : CLmVolatilityModel(CLmFixedVolatilityModel::typeid)
{
#ifdef HANDLE
	_phLmFixedVolatilityModel = NULL;
#endif
	_ppLmFixedVolatilityModel = &childNative;
    _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmFixedVolatilityModel));
}
Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::CLmFixedVolatilityModel (QuantLib::LmFixedVolatilityModel& childNative, Object^ owner) : CLmVolatilityModel(CLmFixedVolatilityModel::typeid)
{
#ifdef HANDLE
	_phLmFixedVolatilityModel = NULL;
#endif
	_ppLmFixedVolatilityModel = new boost::shared_ptr<QuantLib::LmFixedVolatilityModel> (&childNative);
    _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmFixedVolatilityModel));
    _LmFixedVolatilityModelOwner = owner;
    _LmVolatilityModelOwner = owner;
}

Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::CLmFixedVolatilityModel (CLmFixedVolatilityModel^ copy) : CLmVolatilityModel(CLmFixedVolatilityModel::typeid)
{
#ifdef HANDLE
	_phLmFixedVolatilityModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLmFixedVolatilityModel = new boost::shared_ptr<QuantLib::LmFixedVolatilityModel> (copy->GetShared());
        _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmFixedVolatilityModel));
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::CLmFixedVolatilityModel (PLATFORM::Type^ t) : CLmVolatilityModel(CLmFixedVolatilityModel::typeid)
{
#ifdef HANDLE
	_phLmFixedVolatilityModel = NULL;
#endif
	if (!t->IsSubclassOf(CLmFixedVolatilityModel::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::CLmFixedVolatilityModel (QuantLib::Handle<QuantLib::LmFixedVolatilityModel>& childNative, Object^ owner)  : CLmVolatilityModel(CLmFixedVolatilityModel::typeid)
{
	_phLmFixedVolatilityModel = &childNative;
	_ppLmFixedVolatilityModel = &static_cast<boost::shared_ptr<QuantLib::LmFixedVolatilityModel>>(childNative.currentLink());
    _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmFixedVolatilityModel));
    _LmFixedVolatilityModelOwner = owner;
}
Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::CLmFixedVolatilityModel (QuantLib::Handle<QuantLib::LmFixedVolatilityModel> childNative)  : CLmVolatilityModel(CLmFixedVolatilityModel::typeid)
{
	_phLmFixedVolatilityModel = &childNative;
	_ppLmFixedVolatilityModel = &static_cast<boost::shared_ptr<QuantLib::LmFixedVolatilityModel>>(childNative.currentLink());
    _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmFixedVolatilityModel));
}
#endif
#ifdef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::CLmFixedVolatilityModel (QuantLib::LmFixedVolatilityModel childNative)  : CLmVolatilityModel(CLmFixedVolatilityModel::typeid)
{
#ifdef HANDLE
	_phLmFixedVolatilityModel = NULL;
#endif
	_ppLmFixedVolatilityModel = new boost::shared_ptr<QuantLib::LmFixedVolatilityModel> (new QuantLib::LmFixedVolatilityModel (childNative));
    _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmFixedVolatilityModel));
}
#endif

Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::~CLmFixedVolatilityModel ()
{
    if (_ppLmFixedVolatilityModel != NULL)
    {
	    delete _ppLmFixedVolatilityModel;
        _ppLmFixedVolatilityModel = NULL;
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::!CLmFixedVolatilityModel ()
{
    if (_ppLmFixedVolatilityModel != NULL)
    {
	    delete _ppLmFixedVolatilityModel;
    }
}
QuantLib::LmFixedVolatilityModel& Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::GetReference ()
{
    if (_ppLmFixedVolatilityModel == NULL) throw REFNEW NativeNullException ();
	return **_ppLmFixedVolatilityModel;
}
boost::shared_ptr<QuantLib::LmFixedVolatilityModel>& Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::GetShared ()
{
    if (_ppLmFixedVolatilityModel == NULL) throw REFNEW NativeNullException ();
	return *_ppLmFixedVolatilityModel;
}
QuantLib::LmFixedVolatilityModel* Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::GetPointer ()
{
    if (_ppLmFixedVolatilityModel == NULL) throw REFNEW NativeNullException ();
	return &**_ppLmFixedVolatilityModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LmFixedVolatilityModel>& Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::GetHandle ()
{
	if (_phLmFixedVolatilityModel == NULL)
	{
		_phLmFixedVolatilityModel = new Handle<QuantLib::LmFixedVolatilityModel> (*_ppLmFixedVolatilityModel);
	}
	return *_phLmFixedVolatilityModel;
}
#endif
bool Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::HasNative () 
{
	return (_ppLmFixedVolatilityModel != NULL);
}

Double Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel::Volatility (UInt64 i, Double t, Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cx;
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppLmFixedVolatilityModel)->volatility ( _i,  _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Legacy::Libormarketmodels::ILmFixedVolatilityModel^ Cephei::QL::Legacy::Libormarketmodels::CLmFixedVolatilityModel_Factory::Create (Cephei::QL::Math::IArray^ volatilities, Cephei::Core::IVector<Double>^ startTimes)
{
    return REFNEW CLmFixedVolatilityModel ( volatilities,  startTimes);
}
